//
//  FilterBarView.m
//  GPUImage_MyDemo
//
//  Created by yuedongkui on 2018/5/22.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "FilterBarView.h"

#import "BottomNavBar.h"

static NSString *kFilterCellReuseIdentifier = @"kFilterCellReuseIdentifier";


@interface FilterBarView ()<UICollectionViewDelegate, UICollectionViewDataSource, BottomNavBarDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *lists;
@property (nonatomic, strong) BottomNavBar *navBar;

@end



@implementation FilterBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor blueColor];
        
        self.lists = @[@"Sepia", @"编辑", @"增强", @"滤镜", @"马赛克", @"涂鸦", @"边框", @"贴纸", @"文字"];
        
        UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
        [collectionViewLayout setItemSize:CGSizeMake(kFilterBarCellWidth, kFilterBarCellHeight)];
        [collectionViewLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        collectionViewLayout.minimumInteritemSpacing = 0;
        collectionViewLayout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[FilterCell class] forCellWithReuseIdentifier:kFilterCellReuseIdentifier];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self).offset(0);
            make.height.mas_equalTo(kFilterBarCellHeight);
        }];
        
        self.navBar = [[BottomNavBar alloc] initWithFrame:CGRectZero];
        self.navBar.delegate = self;
        [self addSubview:self.navBar];
        [self.navBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).offset(0);
            make.top.equalTo(self.collectionView.mas_bottom).offset(0);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.lists.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFilterCellReuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.titleLabel.text = self.lists[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp_h"];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate filteBarView:self didSelectRow:indexPath.row];
}

#pragma mark - BottomNavBarDelegate
- (void)bottomNavBarDidBack:(BottomNavBar *)bar
{
    [self.delegate filterBarDidBack:self];
}

- (void)bottomNavBarDidConfirm:(BottomNavBar *)bar
{
    [self.delegate filterBarDidConfirm:self];
}

@end





@implementation FilterCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
        [self addSubview:self.iconImageView];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self).offset(0);
            make.size.mas_offset(CGSizeMake(kFilterBarCellWidth, kFilterBarCellHeight-20));
        }];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self).offset(0);
            make.top.equalTo(self.iconImageView.mas_bottom);
        }];
    }
    return self;
}

@end
